<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-04 23:19
 *
 * 项目：rm  -  $  - tablesFormType.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\widgets\inputs\inputtypes;

use Lev;
use lev\widgets\inputs\tablesForm;
use modules\levfm\widgets\inputs\formatViewDatas;

!defined('INLEV') && exit('Access Denied LEV');


class tablesFormType extends tablesForm
{

    public static function inputInfo($name, $value, $v)
    {
        return parent::inputInfo($name, $value, $v);  // TODO: Change the autogenerated stub
    }

    public static function inputInfoOneTr($name, $value, $v)
    {
        return parent::inputInfoOneTr($name, $value, $v); // TODO: Change the autogenerated stub
    }

    public static function inputInfoSubnav($name, $value, $v)
    {
        return parent::inputInfoSubnav($name, $value, $v); // TODO: Change the autogenerated stub
    }

    public static function tablesInputs($settings = null, $tablesForm = null)
    {
        $tablesForm === null &&
        $tablesForm = static::getSettings($settings);
        return parent::tablesInputs($settings, $tablesForm); // TODO: Change the autogenerated stub
    }

    public static function getSettings($settings) {
        return Lev::getSettings(Lev::getSettings($settings, 'set_tablesForm'));
    }

    public static function formatViewData($input, $value, $child = 0) {
        return static::showTablesData($input, $value, $child);
    }

    /**
     * 显示表格数据
     * @param $v
     * @param $values
     * @param int $child 是否为二级数据表格
     * @return string
     */
    public static function showTablesData($v, $values, $child = 0)
    {
        $fields = static::tablesInputs($v['settings']);
        if ($v['inputtype'] == 'tabletrForm') unset($fields['id']);
        $tr = '';
        $htm = '<div class="data-xtable table-fields-box"><table><tr>';
        if ($child) {
            $htm .= '<th style="text-align:center;width:40px">子级</th>';
        }
        foreach ($fields as $k => $r) {
            $_style = $r['width'] ? ' style="width:'.$r['width'].'px"' : '';
            $kcl = '';
            $ckK = strtolower(substr($k, -6));
            if ($ckK == 'status') {
                $kcl = ' tab-center wd30';
            }
            $htm.='<th class="kfield_'.$k.$kcl.'" '.$_style.'>'.$r['title'].'</th>';
        }
        !$values && $values = is_array($v['inputvalue']) ?: unserialize($v['inputvalue']);
        if ($values) {
            foreach ($values as $val) {
                $trcontent = static::getShowTableTr($fields, $val, $v);
                if ($child) {
                    $cls = $val['id'].'_'.$v['inputname'];
                    $cldStr = '';
                    $count = 0;
                    if (isset($val['cld__'])) {
                        $count = count($val['cld__']);
                        $xv['inputname'] = 'cld__'.$val['id'].'_'.$v['inputname'].'__'.$v['inputname'];
                        foreach ($val['cld__'] as $cv) {
                            $cldStr .= '<tr class="childBox_'.$cls.'"><td style="text-align:center"><p class="date">&angrt;</p></td>' . static::getShowTableTr($fields, $cv, $xv) . '</tr>';
                        }
                    }
                    $tr.= '<tr class="navBox_'.$cls.'"><td style="text-align:center"><a class="tablesnavOpenChildBtn" data-cls="'.$cls.'">['.$count.']</a></td>'.$trcontent.'</tr>';
                    $tr.= $cldStr;
                }else {
                    $tr .= '<tr>'.$trcontent.'</tr>';
                }
            }
        }
        $htm.= $tr.'</table></div>';
        return $htm;
    }
    public static function getShowTableTr($fields, $val, $v = []) {
        $tr = '';
        foreach ($fields as $k => $r) {
            $valx = Lev::arrv($k, $val, '');
            $tr .= '<td> '.formatViewDatas::run($r, $valx).' </td>';
        }
        return $tr;
    }

    public static function defaultSettings() {
        return array(
                8 => array(
                    'id' => 8,
                    'order' => '0',
                    'title' => 'ID',
                    //'inputname' => 'id',
                    'inputtype' => 'number',
                    'width' => '40',
                    'settings' => '',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
                1 => array(
                    'id' => 1,
                    'order' => '1',
                    'title' => '排序',
                    //'inputname' => 'order',
                    'inputtype' => 'number',
                    'width' => '40',
                    'settings' => '',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
                5 => array(
                    'id' => 5,
                    'order' => '2',
                    'title' => '开关',
                    //'inputname' => 'status',
                    'inputtype' => 'radio',
                    'width' => '40',
                    'settings' => '',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
                3 => array(
                    'id' => 3,
                    'order' => '3',
                    'title' => '显示类型',
                    //'inputname' => 'ShowType',
                    'inputtype' => 'select',
                    'width' => '100',
                    'settings' => '0=图标
1=文字
2=图标和文字',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
                4 => array(
                    'id' => 4,
                    'order' => '4',
                    'title' => '打开方式',
                    //'inputname' => 'target',
                    'inputtype' => 'selectcode',
                    'width' => '100',
                    'settings' => 'lev\\helpers\\SettingsHelper',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
                2 => array(
                    'id' => 2,
                    'order' => '5',
                    'title' => '导航名称',
                    //'inputname' => 'name',
                    'inputtype' => 'text',
                    'width' => '100',
                    'settings' => '',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
                6 => array(
                    'id' => 6,
                    'order' => '6',
                    'title' => '链接地址',
                    //'inputname' => 'link',
                    'inputtype' => 'text',
                    'width' => '',
                    'settings' => '',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
                7 => array(
                    'id' => 7,
                    'order' => '7',
                    'title' => '导航图标',
                    //'inputname' => 'logoupload',
                    'inputtype' => 'uploadimg',
                    'width' => '',
                    'settings' => '',
                    'placeholder' => '#号开头调用icon图标、也可是@别名和url址址',
                    'inputvalue' => '',
                ),
                9 => array(
                    'id' => 9,
                    'order' => '9',
                    'title' => '按钮颜色',
                    //'inputname' => 'bgcolor',
                    'inputtype' => 'select',
                    'width' => '77',
                    'settings' => 'yellow = 金黄色
red    = 红色
blue   = 蓝色
green  = 绿色
gray   = 灰色
black  = 黑色
white  = 白色',
                    'placeholder' => '',
                    'inputvalue' => '',
                ),
            );
    }
}